package com.wwdx.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wwdx.model.MerchantOrderModel;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.model.WwdxWeixinUserModel;
import com.wwdx.model.dto.BaseMerchantOrderDTO;
import com.wwdx.service.MerchantOrderServiceI;
import com.wwdx.utils.ConstantHolder;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 * @date 2017/11/5
 */
@Service
public class MerchantOrderServiceImpl extends BaseServiceImpl<MerchantOrderModel> implements MerchantOrderServiceI {

    @Override
    public MerchantOrderModel getByOrderNo(String orderno) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("orderNo", orderno);
        return this.getByHql("from MerchantOrderModel where orderNo=:orderNo", params);
    }

    @Override
    public BigDecimal getBalanceNum(String merchantId) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("merchantId", merchantId);
        String sql = "select sum(price) as num from merchant_order where merchantId=:merchantId and status = 1";
        List list = findBySql(sql, params);
        Map map = (Map) list.get(0);
        return map.get("num") == null ? new BigDecimal(0L) : (BigDecimal) map.get("num");
    }

    @Override
    public BaseMerchantOrderDTO.MerchantOrderStatisticsDTO findOrderStatistics(String merchantId) {
        logger.info("商户收入统计");
        BaseMerchantOrderDTO.MerchantOrderStatisticsDTO dto = new BaseMerchantOrderDTO.MerchantOrderStatisticsDTO();
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("merchantId", merchantId);
        //今日收入 | 今日订单数
        String sql = "SELECT IFNULL(SUM(price),0) AS incomes,COUNT(id) AS orders FROM merchant_order\n"+
                "WHERE merchantId=:merchantId AND status = 1 AND TO_DAYS(payTime) = TO_DAYS(NOW())";
        List list = findBySql(sql, params);
        Map map = (Map) list.get(0);
        dto.setTodayIncome((BigDecimal) map.get("incomes"));
        dto.setTodayOrder((BigInteger) map.get("orders"));
        //总收入 | 总订单数
        sql = "SELECT IFNULL(SUM(price),0) AS incomes,COUNT(id) AS orders FROM merchant_order\n" +
                "WHERE merchantId=:merchantId AND status = 1";
        list = findBySql(sql, params);
        map = (Map) list.get(0);
        dto.setTotalIncome((BigDecimal) map.get("incomes"));
        dto.setTotalOrder((BigInteger) map.get("orders"));
        return dto;
    }

    @Override
    public List<BaseMerchantOrderDTO.MerchantOrderListDTO> findOrderList(String merchantId) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("merchantId", merchantId);
        String hql = "from MerchantOrderModel where merchantId=:merchantId AND status = 1 AND TO_DAYS(payTime) = TO_DAYS(NOW())";
        List<MerchantOrderModel> orderModels = this.find(hql, params);
        ArrayList<BaseMerchantOrderDTO.MerchantOrderListDTO> list = Lists.newArrayListWithExpectedSize(orderModels.size());
        BaseMerchantOrderDTO.MerchantOrderListDTO dto = new BaseMerchantOrderDTO.MerchantOrderListDTO();
        for (MerchantOrderModel orderModel : orderModels) {
            dto = (BaseMerchantOrderDTO.MerchantOrderListDTO) dto.clone();
            String openId = orderModel.getOpenId();
            WwdxWeixinUserModel wwdxWeixinUserModel = wwdxWeixinUserService.getByOpenId(openId);
            if (wwdxWeixinUserModel != null){
                dto.setUserName(wwdxWeixinUserModel.getNickname());
            }
            Long productId = orderModel.getProductId();
            Integer type = orderModel.getType();
            switch (type){
                //主题
                case ConstantHolder.Constant.THEMETYPE:
                    WwdxThemeModel wwdxThemeModel = wwdxThemeService.getByThemeId(productId);
                    dto.setProductName(wwdxThemeModel.getTitle());
                    break;
                //书籍 | 小课
                case ConstantHolder.Constant.BOOKTYPE:
                case ConstantHolder.Constant.SMALLBOOKTYPE:
                    WwdxBookModel wwdxBookModel = wwdxBookService.getByBookId(productId);
                    dto.setProductName(wwdxBookModel.getBookname());
                    break;
                //会员
                case ConstantHolder.Constant.VIPTYPE:
                    dto.setProductName("会员");
                    break;
                default:
                    continue;
            }
            dto.setProductPrice(orderModel.getPrice());
            dto.setPayTime(orderModel.getPayTime());
            list.add(dto);
        }
        return list;
    }
}